using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Core;

namespace SPHelpCollectionBuilder
{
    public partial class ReferencedCollectionChooser : Form
    {
        private bool _hasChanges = false;

        public bool HasChanges
        {
            get { return _hasChanges; }
            set { _hasChanges = value; }
        }

        private List<HelpItemManifestEntryReferencedCollectionEntry> _referencedCollections = null;

        public List<HelpItemManifestEntryReferencedCollectionEntry> ReferencedCollections
        {
            get { return _referencedCollections; }
            set { _referencedCollections = value; }
        }

        public ReferencedCollectionChooser()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (ReferencedCollections == null && listBoxReferencedCollection.Items.Count > 0)
                ReferencedCollections = new List<HelpItemManifestEntryReferencedCollectionEntry>();

            foreach (object item in listBoxReferencedCollection.Items)
            {
                ReferencedCollections.Add((HelpItemManifestEntryReferencedCollectionEntry)item);
            }
            this.DialogResult = DialogResult.OK;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxSortOrder.Text))
            {
                MessageBox.Show("Enter a sort order", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(textBoxHelpCollectionName.Text))
            {
                MessageBox.Show("Enter a help collection name", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            int sortNumber = int.Parse(textBoxSortOrder.Text);
            HelpItemManifestEntryReferencedCollectionEntry entry = new HelpItemManifestEntryReferencedCollectionEntry();
            entry.SortOrder = sortNumber;
            entry.Value = textBoxHelpCollectionName.Name;

            listBoxReferencedCollection.Items.Add(entry);
            ClearControls();
            this.HasChanges = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void textBoxSortOrder_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void ClearControls()
        {
            textBoxHelpCollectionName.Text = string.Empty;
            textBoxSortOrder.Text = string.Empty;
        }

        private void listBoxReferencedCollection_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRemove.Enabled = true;
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (listBoxReferencedCollection.SelectedItem != null)
            {
                listBoxReferencedCollection.Items.RemoveAt(listBoxReferencedCollection.SelectedIndex);
                this.HasChanges = true;
            }
        }

        public void LoadData()
        {
            if (HCProjectManager.CurrentProject != null && HCProjectManager.CurrentProject.ProjectManifest.referencedCollections != null)
            {
                foreach (HelpItemManifestEntryReferencedCollectionEntry entry in HCProjectManager.CurrentProject.ProjectManifest.referencedCollections)
                {
                    listBoxReferencedCollection.Items.Add(entry);
                }
            }
        }
    }
}